package ar.edu.utn.frba.dds.opendesigners;

import java.util.ArrayList;
import java.util.List;

import com.lanchita.excepciones.EstadoErroneoException;

import ar.edu.utn.frba.dds.opendesigners.exception.ExceptionInvalidAirline;
import ar.edu.utn.frba.dds.opendesigners.exception.ExceptionInvalidSeat;


public class TravelAgent {
	
	private List<AirlinesAdapter> airlinesList;
	
	public TravelAgent() {
		this.airlinesList = new ArrayList<AirlinesAdapter>();
	}

	public void agregarAerolinea(AirlinesAdapter aerolinea){
		this.airlinesList.add(aerolinea);
	}
	
	public SearchResponse search(SearchParam param){
		SearchResponse result = new SearchResponse();
		
		//Simil foreach en otros lenguajes
		//Hago la consulta a cada aerolinea disponible y guardo los datos retornados.
		for(AirlinesAdapter aerolinea : this.airlinesList){
			result.addAll(aerolinea.search(param));
		}
		
		return result;
	}
	
	public void buy(RouteOption option){
		try {
			this.select(option.getAirline()).buy(option);
		} catch (EstadoErroneoException errorBuy) {
			throw new ExceptionInvalidSeat();
		}
	}
		
	private AirlinesAdapter select(String airlineName){
		for (AirlinesAdapter airline : this.airlinesList) {
				if(airline.getAirline() == airlineName)
					return airline;
		}
		throw new ExceptionInvalidAirline();
	}

}
